﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Collections;

namespace YJY_DMZS.SupplyChain
{
    public partial class FrmInStock_scan : XtraForm
    {
        public FrmInStock_scan()
        {
            InitializeComponent();
        }

        public delegate Hashtable ReturnIDEventHandler(Hashtable htb);
        public event ReturnIDEventHandler ReturnID;

        private int OrderTypeID;
        private long OrderID;
        private int OrderTypeID_CGDD;
        private DataTable OrderBody;

        private void FrmInStock_scan_Load(object sender, EventArgs e)
        {
            DMZS.BLL.Vchtype bll = new DMZS.BLL.Vchtype();
            OrderTypeID_CGDD = bll.GetOrderTypeID(DMZS.Model.DlyndxOrder.OrderType.CGDD);
            if (OrderTypeID_CGDD == 0)
            {
                txtBarcode.Enabled = false;
                btnOK.Enabled = false;
                mmoInfo.Text = "获取采购订单类型初始错误";
            }
            else
            {
                txtBarcode.Enabled = true;
                btnOK.Enabled = true;
            }
            OrderID = 0;

            bteSourceOrderCode.Text = "";
            bteSourceOrderCode.Properties.ReadOnly = true;
            bteSourceOrderCode.Properties.Buttons[0].Enabled = false;
            OrderTypeID = 0;
        }

        private void cbbSourceOrderType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbbSourceOrderType.SelectedIndex == 0)
            {
                bteSourceOrderCode.Text = "";
                bteSourceOrderCode.Properties.ReadOnly = true;
                bteSourceOrderCode.Properties.Buttons[0].Enabled = false;
                OrderTypeID = 0;
            }
            else if(cbbSourceOrderType.SelectedIndex == 1)
            {
                OrderTypeID = OrderTypeID_CGDD;
                bteSourceOrderCode.Text = "";
                bteSourceOrderCode.Properties.ReadOnly = false;
                bteSourceOrderCode.Properties.Buttons[0].Enabled = true;
            }
        }

        private void txtBarcode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                ProcessReturn();
                txtBarcode.Text = "";
                txtBarcode.Focus();
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (txtBarcode.Text.Trim().Length == 0) return;

            //无源头单
            if (cbbSourceOrderType.SelectedIndex == 0)
            {
                ProcessReturn();
                txtBarcode.Text = "";
                txtBarcode.Focus();
            }
            else if (OrderID != 0)
            {
                ProcessReturn();
                txtBarcode.Text = "";
                txtBarcode.Focus();
            }
            else
            {
                mmoInfo.Text = "请先选择源头单据";
                bteSourceOrderCode.Focus();
            }
        }

        /// <summary>
        /// 处理扫描结果
        /// </summary>
        /// <returns></returns>
        private bool ProcessReturn()
        {
            DMZS.BLL.Barcode bll = new DMZS.BLL.Barcode();
            DataRow row = bll.GetRecordByCode(txtBarcode.Text.Trim());

            if (row == null)
            {
                mmoInfo.Text = "未找到该条码，请确定该条码属于本系统。";
                return false;
            }


            if (rdgDirection.SelectedIndex == 0)
            {
                //加
                DataRow[] rows = null;
                if (cbbSourceOrderType.SelectedIndex == 1)
                {
                    //有源单
                    rows = OrderBody.Select("ProductID=" + Convert.ToInt64(row["ProductID"]));
                    if (rows.Length == 0)
                    {
                        mmoInfo.Text = "该条码不在源单中";
                        return false;
                    }
                }

                Hashtable htb = new Hashtable();
                htb.Add("Direction", 1);
                htb.Add("ProductID", row["ProductID"]);
                htb.Add("ProductCode", row["ProductCode"]);
                htb.Add("ProductName", row["ProductName"]);
                htb.Add("ID", row["ID"]);
                htb.Add("Code", row["Code"]);
                htb.Add("Status", row["Status"]);
                htb.Add("UnitBaseID", row["UnitBaseID"]);
                htb.Add("UnitBaseCode", row["UnitBaseCode"]);
                htb.Add("UnitBaseName", row["UnitBaseName"]);
                htb.Add("OrderTypeID", OrderTypeID);
                htb.Add("OrderTypeName", (rows != null && rows.Length > 0) ? rows[0]["VchTypeName"] : "");
                htb.Add("OrderID", OrderID);
                htb.Add("OrderLineID", (rows != null && rows.Length > 0) ? rows[0]["DlyOrder"] : 0);
                htb.Add("OrderCode", (rows != null && rows.Length > 0) ? rows[0]["Number"] : "");
                htb.Add("Qty", 1);

                Hashtable result = ReturnID(htb);
                if (Convert.ToInt32(result["ID"]) > 0)
                {
                    mmoInfo.Text = result["Message"].ToString();
                    return true;
                }
                else
                {
                    mmoInfo.Text = result["Message"].ToString();
                    return false;
                }
            }
            else
            {
                //减
                Hashtable htb = new Hashtable();
                htb.Add("Direction", -1);
                htb.Add("Code", row["Code"]);
                htb.Add("ProductCode", row["ProductCode"]);
                htb.Add("ProductID", row["ProductID"]);

                Hashtable result = ReturnID(htb);

                if (Convert.ToInt32(result["ID"]) > 0)
                {
                    mmoInfo.Text = result["Message"].ToString();
                    return true;
                }
                else
                {
                    mmoInfo.Text = result["Message"].ToString();
                    return false;
                }
            }
        }

        private void bteSourceOrderCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DialogForm.FrmPurchaseOrder_dlg frm = new DialogForm.FrmPurchaseOrder_dlg();
            frm.ReturnID += new DialogForm.FrmPurchaseOrder_dlg.ReturnIDEventHandler(frm_ReturnID);
            frm.ShowDialog();
        }

        void frm_ReturnID(long ID)
        {
            DMZS.BLL.PurchaseOrder bll = new DMZS.BLL.PurchaseOrder();
            ProcessSelectOrder(bll.GetBody(ID));
        }

        private void bteSourceOrderCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != '\r') return;

            if (bteSourceOrderCode.Text.Trim().Length == 0)
            {
                OrderID = 0;
                OrderBody = null;
                return;
            }
            DMZS.BLL.PurchaseOrder bll = new DMZS.BLL.PurchaseOrder();
            ProcessSelectOrder(bll.GetBody(bteSourceOrderCode.Text.Trim()));
        }

        /// <summary>
        /// 处理源头单的选择结果
        /// </summary>
        /// <param name="OrderBody"></param>
        /// <returns></returns>
        private bool ProcessSelectOrder(DataTable OrderBody)
        {
            if (OrderBody == null)
            {
                mmoInfo.Text = "获取订单信息失败";
                return false;
            }
            else if (OrderBody.Rows.Count == 0)
            {
                mmoInfo.Text = "该订单没有表体行";
                return false;
            }
            else if (Convert.ToInt32(OrderBody.Rows[0]["draft"]) == 0)
            {
                mmoInfo.Text = "该订单未审核，请选择已审记录";
                return false;
            }
            else if (Convert.ToInt32(OrderBody.Rows[0]["draft"]) == 2)
            {
                mmoInfo.Text = "该订单已关闭，请选择待审记录";
                return false;
            }

            OrderID = Convert.ToInt64(OrderBody.Rows[0]["Vchcode"]);
            this.OrderBody = OrderBody;
            bteSourceOrderCode.Text = OrderBody.Rows[0]["Number"].ToString();
            return true;
        }

        private void bteSourceOrderCode_Leave(object sender, EventArgs e)
        {
            if (cbbSourceOrderType.SelectedIndex != 0 && OrderBody != null)
            {
                bteSourceOrderCode.Text = OrderBody.Rows[0]["Number"].ToString();
            }
        }

        private void txtBarcode_Enter(object sender, EventArgs e)
        {
            txtBarcode.Text = "";
        }

    }
}
